package cn.fenqing168.springcloud.controller;

import cn.fenqing168.springcloud.service.IPaymentFeignService;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * @author fenqing
 */
@RestController
public class TestController {

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    private IPaymentFeignService iPaymentFeignService;

    public static final String PAYMENT_BASE_URL = "http://cloudalibaba-provider-payment";

    @GetMapping("/test")
    //@SentinelResource(value = "test", fallback = "fallbackTest")
    //@SentinelResource(value = "test", blockHandler = "blockTest")
    //@SentinelResource(value = "test", blockHandler = "blockTest", fallback = "fallbackTest", exceptionsToIgnore = RuntimeException.class)
    public String test(Integer id){
        if(id == null){
            return iPaymentFeignService.test();
        }else if(id == 4){
            throw new RuntimeException("非法参数异常");
        }else {
            throw new RuntimeException("没有查询到数据");
        }
    }

    public String fallbackTest(Integer id, Throwable e){
        return "程序发生异常！🆔" + id + ",异常信息为：" + e.getClass().getCanonicalName() + ",异常消息为：" + e.getMessage();
    }

    public String blockTest(Integer id, BlockException e){
        return "触发了限流！🆔" + id + ",异常信息为：" + e.getClass().getCanonicalName() + ",异常消息为：" + e.getMessage();
    }





}
